<template>
  <el-card>
    <el-row :gutter="20" class="header">
      <el-col :span="4" style="margin: auto;font-size: 21px;font-family: '华文仿宋';color: red;">
        待处理工单
      </el-col>
    </el-row>
    <el-row class="header">
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="checkin.type" label="维修或保养">
          <template #default="{ row }">
            <el-tag type="warning" effect="dark" v-if="row.checkin.type=='1'">维修</el-tag>
            <el-tag type="success" effect="dark" v-if="row.checkin.type=='2'">保养</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="checkin.name" label="车主姓名" />
        <el-table-column prop="user.nickname" label="指派员工" />
        <el-table-column prop="assignTime" label="指派时间" :formatter="formatTime" />
        <el-table-column prop="createTime" label="创建时间" :formatter="formatTime" />
        <el-table-column label="操作" width="200">
          <template #default="{}">
            <el-button type="primary" @click="handleRedirect()">前往处理</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <el-pagination
      v-model:current-page="queryForm.pageNum"
      v-model:page-size="queryForm.pageSize"
      :page-sizes="[10, 20, 30, 50]"
      layout="prev, pager, next, jumper, sizes, total"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :background="true"
    />
  </el-card>
  <Dialog v-model="dialogVisible"
          :dialogTitle="dialogTitle"
          v-if="dialogVisible"
          @query="query"
          :dialogRowValue="dialogRowValue" />
  <UserDialog v-model="UserVisible"
              :dialogTitle="UserTitle"
              v-if="UserVisible"
              @query="query"
              :dialogRowValue="UserRowValue" />
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { list } from '../api/workorder'
import jwtDecode from 'jwt-decode'
import router from '../router'

const queryForm = ref({
  userId: '',
  pageNum: 1,
  pageSize: 10
})
const tableData = ref([])
const total = ref(0)
const dialogVisible = ref(false)
const dialogTitle = ref('')
const dialogRowValue = ref({})

const UserVisible = ref(false)
const UserTitle = ref('')
const UserRowValue = ref({})

// 前往处理工单
const handleRedirect = () => {
  router.replace('/workorder')
}

async function query() {
  const resp = await list(queryForm.value)
  tableData.value = resp.data.data.list
  total.value = resp.data.data.total
}

onMounted(() => {
  // 获取token
  const token = localStorage.getItem('token')
  // 解析获取token
  const decode = jwtDecode(token).id // 解析
  // 保存accoundId
  queryForm.value.userId = decode
  query()
})

function handleCurrentChange(pageNum) {
  queryForm.value.pageNum = pageNum
  query()
}

function handleSizeChange(pageSize) {
  queryForm.value.pageNum = 1
  queryForm.value.pageSize = pageSize
  query()
}

function formatTime(row, column) {
  const date = new Date(row[column.property])
  const year = date.getFullYear()
  let month = date.getMonth() + 1
  if (month < 10) {
    month = '0' + month
  }
  let day = date.getDate()
  if (day < 10) {
    day = '0' + day
  }
  return year + '-' + month + '-' + day
}
</script>

<style lang="scss" scoped>
.header {
  padding-bottom: 16px;
  box-sizing: border-box;
}
::v-deep .el-input__suffix {
  align-items: center;
}
</style>
